package BookStore.view;

import BookStore.dao.LoginDao;
import BookStore.model.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LoginFrame extends JFrame{
    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JRadioButton wayField1;
    private JRadioButton wayField2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    LoginFrame frame = new LoginFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginFrame() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(500, 250, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("书店销售管理系统");
        lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
        lblNewLabel.setForeground(Color.RED);
        lblNewLabel.setBounds(133, 30, 170, 29);
        contentPane.add(lblNewLabel);

        wayField1 = new JRadioButton("管理员登录");
        wayField2 = new JRadioButton("销售员登录");
        JPanel jp1=new JPanel();
        jp1.setBorder(BorderFactory.createTitledBorder("请选择登录方式"));
        jp1.setSize(120,80);
        ButtonGroup group = new ButtonGroup();
        group.add(wayField1);
        group.add(wayField2);
        jp1.add(wayField1);
        jp1.add(wayField2);
        contentPane.add(jp1,BorderLayout.NORTH);




        JLabel label = new JLabel("用户名：");
        label.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
        label.setBounds(93, 106, 66, 19);//在输入框前面打印“用户名”
        contentPane.add(label);

        textField = new JTextField();
        textField.setBounds(171, 103, 132, 26);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_1 = new JLabel("密  码：");
        label_1.setBounds(98, 137, 61, 16);
        label_1.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
        contentPane.add(label_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(171, 132, 132, 26);
        contentPane.add(passwordField);

        JButton button = new JButton("登陆");
        button.setBounds(93, 192, 99, 29);
        contentPane.add(button);

        JButton button_1 = new JButton("注册");
        button_1.setBounds(253, 192, 99, 29);
        contentPane.add(button_1);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String t1 = textField.getText();
                String p1 = String.valueOf(passwordField.getPassword());
                String way1 = textField.getText();
                String way2 = textField.getText();
                User user = new User();
                user.setUserName(t1);
                user.setUserPsw(p1);
                //user.setUserBos(way1);
                //user.setUserSta(way2);
                try {
                    new LoginDao(user).ligin();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new Register().result();
            }
        });
    }
}
